package cz.cvut.liy;

import java.util.ArrayList;
import java.util.List;

import cz.cvut.liy.adapter.TestAdapter;
import cz.cvut.liy.data.Question;
import cz.cvut.liy.data.Test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;

public class FirstActivity extends Activity{

	List<Test> tests;
	TestAdapter ta;
	ExpandableListView ev;
	Button button;
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.first);
        
        Test test = new Test("DBA");
        
        
        test.addQuestion(new Question("otazka", "odpoved"));
        test.addQuestion(new Question("otazka1", "odpoved"));
        test.addQuestion(new Question("otazka2", "odpoved"));
        test.addQuestion(new Question("otazka3", "odpoved"));
        test.addQuestion(new Question("otazka4", "odpoved"));
        test.addQuestion(new Question("otazka5", "odpoved"));
        
        
        ev = (ExpandableListView) findViewById(R.id.courseList);
        tests = Test.loadFile(this);
        tests.add(test);
        tests.add(new Test("DSA"));
        ta = new TestAdapter(tests, this);
        ev.setAdapter(ta);
        
        ev.setOnChildClickListener(new startActivity());
        
    }
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Test.saveFile(tests, this);
	}

  private class startActivity implements OnChildClickListener {

	@Override
	public boolean onChildClick(ExpandableListView parent, View v,
			int groupPosition, int childPosition, long id) {
		
		Test test = (Test)ta.getGroup(groupPosition);
		if (childPosition > 0 && !test.isStarted()) childPosition++;
		// TODO Auto-generated method stub
		if (childPosition == 0 || childPosition == 1) {
			Intent intent = new Intent().setClass(FirstActivity.this, TestActivity.class);
			if (childPosition == 0) test.initAnsweredQuestions();
			test.setStarted(true);
			Test.setStartTest(test);
			startActivity(intent);
		}
		else if (childPosition == 3) tests.remove(0);
		else if (childPosition == 2){
			Intent intent = new Intent().setClass(FirstActivity.this, StatisticActivity.class);
			Test.setStartTest((Test)ta.getGroup(groupPosition));
			startActivity(intent);
		}
		ta.notifyDataSetChanged();
		return false;
	}
	  
  }
}
